package com.youkeyi.ddy.cloud.application.business.crm.customer.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.BusinessCustomerFollowCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.dto.BusinessCustomerFollowListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.dto.BusinessCustomerFollowListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessCustomerFollowCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessCustomerFollowDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessCustomerFollowListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.customer.param.BusinessCustomerFollowUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.CrmCustomerFollowUpCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.dto.CrmCustomerFollowUpListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.follow.param.CrmCustomerFollowUpUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.PublicTodoCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.todo.param.PublicTodoCreateCloudParam;
import com.youkeyi.ddy.cloud.common.constant.YoukeyiDdyCloudBasicCrmConstant;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import com.youkeyi.ddy.cloud.common.utils.SystemDateUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCustomerFollowController
 *
 * @Author liuxiawang
 * @Date 2024/4/1 5:51 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerFollowController implements BusinessCustomerFollowCloudService {
    
    @Autowired
    private CrmCustomerFollowUpCloudService crmCustomerFollowUpCloudService;
    @Autowired
    private PublicTodoCloudService publicTodoCloudService;

    @Override
    public BusinessCustomerFollowListCloudDto list(BusinessCustomerFollowListCloudParam param) {
        BusinessCustomerFollowListCloudDto dto = new BusinessCustomerFollowListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerFollowUpQueryCloudParam followUpQueryCloudParam = new CrmCustomerFollowUpQueryCloudParam();
        followUpQueryCloudParam.setCustomerFollowUpPrimaryKeys(param.getCustomerFollowUpPrimaryKeys());
        followUpQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        followUpQueryCloudParam.setCustomerKeys(param.getCustomerKeys());
        followUpQueryCloudParam.setStatuses(param.getStatuses());
        followUpQueryCloudParam.setPageSize(param.getPageSize());
        followUpQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerFollowUpListCloudDto followUpDtos = crmCustomerFollowUpCloudService.query(followUpQueryCloudParam);
        if (null != followUpDtos && CollectionUtils.isNotEmpty(followUpDtos.getRows())) {
            List<BusinessCustomerFollowListItemCloudDto> items = new ArrayList<>();
            followUpDtos.getRows().forEach(followUp -> {
                BusinessCustomerFollowListItemCloudDto item = new BusinessCustomerFollowListItemCloudDto();
                item.setCustomerFollowUpPrimaryKey(followUp.getCustomerFollowUpPrimaryKey());
                item.setCorpKey(followUp.getCorpKey());
                item.setCustomerKey(followUp.getCustomerKey());
                item.setFollowUp(followUp.getFollowUp());
                item.setFollowUpDate(followUp.getFollowUpDate());
                item.setNextFollowUpRemindDate(followUp.getNextFollowUpRemindDate());
                item.setRemindStart(followUp.getRemindStart());
                item.setRemark(followUp.getRemark());
                item.setStatus(followUp.getStatus());
                item.setStatusName(YoukeyiDdyCloudBasicCrmConstant.CRM_CUSTOMER_FOLLOW_STATUS.getName(followUp.getStatus()));
                item.setCustomerTypeKey(followUp.getCustomerTypeKey());
                item.setServerDate(followUp.getServerDate());
                item.setAssistingPersonnelKey(followUp.getAssistingPersonnelKey());
                item.setAssistingPersonnelName(followUp.getAssistingPersonnelName());
                item.setIsDeleted(followUp.getIsDeleted());
                item.setIsDeletedName(followUp.getIsDeletedName());
                item.setCreateUserKey(followUp.getCreateUserKey());
                item.setCreateUserName(followUp.getCreateUserName());
                item.setCreateTime(followUp.getCreateTime());
                item.setUpdateUserKey(followUp.getUpdateUserKey());
                item.setUpdateUserName(followUp.getUpdateUserName());
                item.setUpdateTime(followUp.getUpdateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(followUpDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(BusinessCustomerFollowCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerFollowUpCreateCloudParam followUpCreateCloudParam = new CrmCustomerFollowUpCreateCloudParam();
        followUpCreateCloudParam.setCorpKey(corpKey);
        followUpCreateCloudParam.setCustomerKey(param.getCustomerKey());
        followUpCreateCloudParam.setFollowUp(param.getFollowUp());
        followUpCreateCloudParam.setFollowUpDate(param.getFollowUpDate());
        followUpCreateCloudParam.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate());
        followUpCreateCloudParam.setRemindStart(param.getRemindStart());
        followUpCreateCloudParam.setRemark(param.getRemark());
        followUpCreateCloudParam.setStatus(param.getStatus());
        followUpCreateCloudParam.setCustomerTypeKey(param.getCustomerTypeKey());
        followUpCreateCloudParam.setServerDate(param.getServerDate());
        followUpCreateCloudParam.setAssistingPersonnelName(param.getAssistingPersonnelName());
        followUpCreateCloudParam.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        followUpCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        followUpCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        ResponseContent responseContent = crmCustomerFollowUpCloudService.create(followUpCreateCloudParam);
        if (null != responseContent && responseContent.isSuccess()) {

            String content = String.format(
                    "待办内容：%s，下次跟进提醒时间为：%s，提醒级别 %s星",
                    param.getFollowUp(),
                    SystemDateUtils.ofPattern(param.getNextFollowUpRemindDate(), "yyyy-MM-dd HH:mm:ss"),
                    param.getRemindStart());

            PublicTodoCreateCloudParam todoCreateCloudParam = new PublicTodoCreateCloudParam();
            todoCreateCloudParam.setCorpKey(YoukeyiSystemContext.getCorpPrimaryKey());
            todoCreateCloudParam.setUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCustomerKey(param.getCustomerKey());
            todoCreateCloudParam.setContent(content);
            todoCreateCloudParam.setStatus(1);
            todoCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
            todoCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
            publicTodoCloudService.create(todoCreateCloudParam);
        }
        rc.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return rc;
    }

    @Override
    public ResponseContent update(BusinessCustomerFollowUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerFollowUpPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerFollowUpUpdateCloudParam followUpUpdateCloudParam = new CrmCustomerFollowUpUpdateCloudParam();
        followUpUpdateCloudParam.setCustomerFollowUpPrimaryKey(param.getCustomerFollowUpPrimaryKey());
        followUpUpdateCloudParam.setCorpKey(corpKey);
        followUpUpdateCloudParam.setFollowUp(param.getFollowUp());
        followUpUpdateCloudParam.setFollowUpDate(param.getFollowUpDate());
        followUpUpdateCloudParam.setNextFollowUpRemindDate(param.getNextFollowUpRemindDate());
        followUpUpdateCloudParam.setRemindStart(param.getRemindStart());
        followUpUpdateCloudParam.setRemark(param.getRemark());
        followUpUpdateCloudParam.setStatus(param.getStatus());
        followUpUpdateCloudParam.setCustomerTypeKey(param.getCustomerTypeKey());
        followUpUpdateCloudParam.setServerDate(param.getServerDate());
        followUpUpdateCloudParam.setAssistingPersonnelName(param.getAssistingPersonnelName());
        followUpUpdateCloudParam.setAssistingPersonnelKey(param.getAssistingPersonnelKey());
        followUpUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        followUpUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerFollowUpCloudService.update(followUpUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(BusinessCustomerFollowDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getCustomerFollowUpPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerFollowUpDeleteCloudParam followUpDeleteCloudParam = new CrmCustomerFollowUpDeleteCloudParam();
        followUpDeleteCloudParam.setCustomerFollowUpPrimaryKeys(param.getCustomerFollowUpPrimaryKeys());
        followUpDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        followUpDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerFollowUpCloudService.delete(followUpDeleteCloudParam);
    }
}
